home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DDJ0992.ARJ / COLOR.C < prev    next >
Text File  |  1992-03-31  |  2KB  |  46 lines

  1. /* Color handling routines. The current color model is the RGB color cube. */
  2.  
  3. #include <stdlib.h>
  4. #include "polygon.h"
  5.  
  6. /* Converts a model color (a color in the RGB color cube, in the current
  7.    color model) to a color index for mode X. Pure primary colors are
  8.    special-cased, and everything else is handled by a 2-2-2 model. */
  9. int ModelColorToColorIndex(ModelColor * Color)
  10. {
  11.    if (Color->Red == 0) {
  12.       if (Color->Green == 0) {
  13.          /* Pure blue */
  14.          return(192+(Color->Blue >> 2));
  15.       } else if (Color->Blue == 0) {
  16.          /* Pure green */
  17.          return(128+(Color->Green >> 2));
  18.       }
  19.    } else if ((Color->Green == 0) && (Color->Blue == 0)) {
  20.       /* Pure red */
  21.       return(64+(Color->Red >> 2));
  22.    }
  23.    /* Multi-color mix; look up the index with the two most significant bits
  24.       of each color component */
  25.    return(((Color->Red & 0xC0) >> 2) | ((Color->Green & 0xC0) >> 4) |
  26.          ((Color->Blue & 0xC0) >> 6));
  27. }
  28.  
  29. /* Adjusts the red, green, and blue components of a color to the specified
  30.    fractions of full intensity, limiting colors to no more than the brightest
  31.    valid color. Note that in this case FixedMul is actually being used to
  32.    multiply colors in 32.0 format by intensities in 16.16 format, generating
  33.    a 32.0 result, of which the 8 lower bits are the adjusted color. Casting
  34.    Source->Red to Fixedpoint effectively just stretches it a long, putting it
  35.    in 32.0 format for FixedMul rather than its normal 8.0 format. */
  36. void IntensityAdjustColor(ModelColor * Dest, ModelColor * Source,
  37.    ModelIntensity * Intensity)
  38. {
  39.    Dest->Red = min(FixedMul((Fixedpoint)Source->Red, Intensity->Red), 255);
  40.    Dest->Green = min(FixedMul((Fixedpoint)Source->Green, Intensity->Green),
  41.          255);
  42.    Dest->Blue = min(FixedMul((Fixedpoint)Source->Blue, Intensity->Blue),
  43.          255);
  44. }
  45.  
  46.